<?php
class WindowAction extends CommonAction {
	public function _initialize(){
		parent::_initialize();
		$this->listorder = 'sort';
		//按照约定新建数据模型实例
		$model = $this->_get('tablename');
		$this->dao = D($model);
		//如果有多级关系的表,则输出父ID
		if($this->_get('parent_id')){
		    $this -> assign('parent_id', $this->_get('parent_id'));
		}
		if($this->_get('modules_id')){
		    $this -> assign('modules_id', $this->_get('modules_id'));
		}else{
			$this->error('模块ID错误');
		}
	}
	public function delete(){
		parent::delete();
	}
	
	public function dosort(){
		parent::dosort($this->_get('tablename'));
	} 
	
	//建立表单
	public function edit(){
		//查询模型
		$m = M('Manage_module');
		$fieldlist = explode(',', $this->_get('fieldlist'));
		//模型查询条件
		$map['fieldname'] = array('in', $fieldlist);
		$map['tablename'] = C('DB_PREFIX').$this->_get('tablename');
		$list = $m -> where($map) -> select();
		foreach($list as $kk=>$vv){
			$list[$kk]['_select']=json_decode($vv['_select']);
		}
		
		//查询数据
		$this->assign('tablename', $this->_get('tablename'));
		$datamap = $this -> parseMap($this->_get('map'));
		$data = $this->dao -> where($datamap) ->find();
		
		if($this->_get('tablename') == 'Nav'){
			
			    $data['link'] = str_replace('__APP__','',$data['link']);

		}
		
		$this->assign('submittype', 'update');
		$this -> assign('cur_setting', $data);
		$this -> assign('list', $list);
		$this->display('Window:edit');
	}
	
	//建立表单
	public function add(){
		//查询模型
		$m = M('Manage_module');
		$fieldlist = explode(',', $this->_get('fieldlist'));
		//模型查询条件
		$map['fieldname'] = array('in', $fieldlist);
		$map['tablename'] = C('DB_PREFIX').$this->_get('tablename');
		$list = $m -> where($map) -> select();
		foreach($list as $kk=>$vv){
				$list[$kk]['_select']=json_decode($vv['_select']);
			}
		
		//查询数据
		$this->assign('tablename', $this->_get('tablename'));
		$datamap = $this -> parseMap($this->_get('map'));
		//$data = $this->dao -> where($datamap) ->find();
		$this->assign('submittype', 'insert');
		//$this -> assign('cur_setting', $data);
		$this -> assign('list', $list);
		$this->display('Window:edit');
	}
	//分析GET回来的条件
	private function parseMap($mapstr){
		$arr = explode('__', $mapstr);
		$ret = array();
		foreach($arr as $k => $v){
			$r = explode(',', $v);
			$ret[$r[0]] = $r[1];
		}
		return $ret;
	}
	//列表
	public function index(){
		//查询模型
		
		$m = M('Manage_module');
		$fieldlist = explode(',', $this->_get('fieldlist'));
		//模型查询条件
		$map['fieldname'] = array('in', $fieldlist);
		$map['tablename'] = C('DB_PREFIX').$this->_get('tablename');
		$list = $m -> where($map) -> select();
		foreach($list as $kk=>$vv){
			$list[$kk]['_select']=json_decode($vv['_select']);
		}
		
		//查询数据
		$this->assign('tablename', $this->_get('tablename'));
		
		
		$this->listmap = $this -> parseMap($this->_get('map'));
		$this->listmap['site_id'] = $this->siteid;
		//导航和分类有多级关系,加一个PID作为条件查询
		if($this->_get('tablename') == 'Category' || $this->_get('tablename') == 'Nav'){
			$this->listmap['pid'] = 0;
		}
		$this->_list();
		if($this->_get('tablename') == 'Category' || $this->_get('tablename') == 'Nav'){
			
			
			
			$data = array();
			foreach($this->get('list') as $v){
				$v['sub'] = $this->dao->getListToArray1($v['id'],$this->siteid);
				$data[] = $v;
			}
			$this->assign('list', $data);
		}
		$this -> assign('cellcount', count($fieldlist)+2);
		$this -> assign('fieldlist', $this->_get('fieldlist'));
		$this -> assign('fields', $list);
		$this->display('Window:index');
	}
}